package jehc.djshi.sys.service.impl;
import java.util.*;
import cn.hutool.core.collection.CollectionUtil;
import jehc.djshi.common.base.BaseService;
import jehc.djshi.common.util.ExceptionUtil;
import jehc.djshi.sys.model.XtPlatformFeedback;
import org.springframework.stereotype.Service;
import jehc.djshi.sys.service.XtPlatformFeedbackService;
import jehc.djshi.common.util.StringUtil;
import jehc.djshi.sys.service.XtPlatformService;
import jehc.djshi.sys.dao.XtPlatformDao;
import jehc.djshi.sys.model.XtPlatform;
import javax.annotation.Resource;
/**
 * @Desc 平台信息发布
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class XtPlatformServiceImpl extends BaseService implements XtPlatformService{

	@Resource
	private XtPlatformDao xtPlatformDao;

	@Resource
	private XtPlatformFeedbackService xtPlatformFeedbackService;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<XtPlatform> getXtPlatformListByCondition(Map<String,Object> condition){
		try{
			return xtPlatformDao.getXtPlatformListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public XtPlatform getXtPlatformById(String id){
		try{
			XtPlatform xtPlatform = xtPlatformDao.getXtPlatformById(id);
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("platformId", id);
			List<XtPlatformFeedback> xtPlatformFeedback = xtPlatformFeedbackService.getXtPlatformFeedbackListByCondition(condition);
			xtPlatform.setXtPlatformFeedback(xtPlatformFeedback);
			return xtPlatform;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param xtPlatform 
	* @return
	*/
	public int addXtPlatform(XtPlatform xtPlatform){
		int i = 0;
		try {
			Date date = getDate();
			xtPlatform.setCreateId(getXtUid());
			xtPlatform.setCreateTime(date);
			i = xtPlatformDao.addXtPlatform(xtPlatform);
			List<XtPlatformFeedback> xtPlatformFeedbackList = xtPlatform.getXtPlatformFeedback();
			if(CollectionUtil.isNotEmpty(xtPlatformFeedbackList)){
				for(XtPlatformFeedback platformFeedback: xtPlatformFeedbackList){
					platformFeedback.setCreateId(xtPlatform.getCreateId());
					platformFeedback.setCreateTime(date);
					platformFeedback.setPlatformId(xtPlatform.getId());
					platformFeedback.setId(toUUID());
				}
			}
			if(!xtPlatformFeedbackList.isEmpty()&&xtPlatformFeedbackList.size()>0){
				xtPlatformFeedbackService.addBatchXtPlatformFeedback(xtPlatformFeedbackList);
			}
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param xtPlatform 
	* @return
	*/
	public int updateXtPlatform(XtPlatform xtPlatform){
		int i = 0;
		try {
			xtPlatform.setUpdateId(getXtUid());
			xtPlatform.setUpdateTime(getDate());
			i = xtPlatformDao.updateXtPlatform(xtPlatform);
			List<XtPlatformFeedback> xtPlatformFeedbackList = xtPlatform.getXtPlatformFeedback();
			List<XtPlatformFeedback> xtPlatformFeedbackAddList = new ArrayList<XtPlatformFeedback>();
			List<XtPlatformFeedback> xtPlatformFeedbackUpdateList = new ArrayList<XtPlatformFeedback>();
			if(CollectionUtil.isNotEmpty(xtPlatformFeedbackList)){
				for(int j = 0; j < xtPlatformFeedbackList.size(); j++){
					xtPlatformFeedbackList.get(j).setPlatformId(xtPlatform.getId());
					if(StringUtil.isEmpty(xtPlatformFeedbackList.get(j).getId())){
						xtPlatformFeedbackList.get(j).setId(toUUID());
						xtPlatformFeedbackList.get(j).setUpdateId(xtPlatform.getUpdateId());
						xtPlatformFeedbackList.get(j).setCreateTime(getDate());
						xtPlatformFeedbackAddList.add(xtPlatformFeedbackList.get(j));
					}else{
						xtPlatformFeedbackList.get(j).setUpdateTime(xtPlatform.getUpdateTime());
						xtPlatformFeedbackList.get(j).setUpdateId(xtPlatform.getUpdateId());
						xtPlatformFeedbackUpdateList.add(xtPlatformFeedbackList.get(j));
					}
				}
			}
			if(!xtPlatformFeedbackAddList.isEmpty()&&xtPlatformFeedbackAddList.size()>0){
				xtPlatformFeedbackService.addBatchXtPlatformFeedback(xtPlatformFeedbackAddList);
			}
			if(!xtPlatformFeedbackUpdateList.isEmpty()&&xtPlatformFeedbackUpdateList.size()>0){
				xtPlatformFeedbackService.updateBatchXtPlatformFeedback(xtPlatformFeedbackUpdateList);
			}
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delXtPlatform(Map<String,Object> condition){
		int i = 0;
		try {
			i = xtPlatformDao.delXtPlatform(condition);
			String[] platformIdList= (String[])condition.get("platformId");
			for(String platformId:platformIdList){
				xtPlatformFeedbackService.delXtPlatformFeedbackByForeignKey(platformId);
			}
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量添加
	* @param xtPlatformList 
	* @return
	*/
	public int addBatchXtPlatform(List<XtPlatform> xtPlatformList){
		int i = 0;
		try {
			i = xtPlatformDao.addBatchXtPlatform(xtPlatformList);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量修改
	* @param xtPlatformList 
	* @return
	*/
	public int updateBatchXtPlatform(List<XtPlatform> xtPlatformList){
		int i = 0;
		try {
			i = xtPlatformDao.updateBatchXtPlatform(xtPlatformList);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量修改（根据动态条件）
	* @param xtPlatformList 
	* @return
	*/
	public int updateBatchXtPlatformBySelective(List<XtPlatform> xtPlatformList){
		int i = 0;
		try {
			i = xtPlatformDao.updateBatchXtPlatformBySelective(xtPlatformList);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}
}
